package com.lsh.oms.core.service.order.frozen;

import com.lsh.oms.core.service.order.AbstractOrderService;
import org.springframework.stereotype.Service;

/**
 * 订单发货service
 *
 * @author peter
 */
@Service
@Deprecated
public class OrderReturnFrozenService extends AbstractOrderService {

//    private static Logger logger = LoggerFactory.getLogger(OrderReturnFrozenService.class);
//
//    @Autowired
//    private RoOrderService roOrderService;
//
//    @Autowired
//    private EsProxy esProxy;
//
//    @Autowired
//    private ShippingOrderService shippingService;
//
//    @Autowired
//    private SignOrderService signOrderService;

    /**
     * 创建返仓单
     *
     * @param orderHead 订单头信息
     * @param logSign   唯一标记
     * @return 返回值
     * @throws Exception 异常信息
     */
//    @Transactional
//    public CommonResult<Object> createROOrder(OrderHead orderHead, String logSign) throws Exception {
//
//        OrderShippingHeadWithBLOBs orderShippingHeadWithBLOBs = shippingService.getBolbs(orderHead.getOrderCode());
//        if (orderShippingHeadWithBLOBs == null) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "此订单未生成发货单");
//        }
//
//        OrderRoHead orderRoHeadFormDb = roOrderService.getHead(orderHead.getOrderCode());
//
//        OrderSignHead orderSignHead = signOrderService.getSignHeadByOrderId(orderHead.getOrderCode(),false);
//        if (orderSignHead != null) {
//            if (orderSignHead.getPayStatus() != null && orderSignHead.getPayStatus() == 2) {
//                throw new BusinessException(ReturnCode.REQUEST_FAIL, "此订单已生成签收单,签收单已支付,不能全单返仓");
//            } else {
//                this.validOrderSignEs(orderSignHead, logSign);
//
//                if (orderRoHeadFormDb != null) {
//                    this.validOrderRoEs(orderRoHeadFormDb, logSign);
//                }
//            }
//        } else {
//            if (orderRoHeadFormDb != null) {
//                throw new BusinessException(ReturnCode.REQUEST_SUCCESS, "此订单已经生成返仓.请查询");
//            }
//        }
//
//        Long orderCode = orderHead.getOrderCode();
//
//        OrderRoHead orderRoHead = this.createOrderRoHead(orderShippingHeadWithBLOBs, orderHead);
//        List<OrderRoDetail> orderRoDetailList = this.createOrderRoDetails(orderShippingHeadWithBLOBs, orderRoHead);
//
//        int num = 0;
//        for (int i = 0; i < 2; i++) {
//            try {
//                num = roOrderService.create(orderRoHead, orderRoDetailList, true);
//                break;
//            } catch (Exception e) { //失败重试一次
//                logger.warn("【" + orderCode + "】创建返仓单失败" + e.getMessage(), e);
//            }
//        }
//
//        this.notifyEs(orderHead.getOrderCode(), logSign, orderShippingHeadWithBLOBs.getId(), true);
//
//        CommonResult<Object> res = new CommonResult<>();
//        res.setCode(ReturnCode.REQUEST_SUCCESS);
//        res.setData(num > 0);
//        return res;
//    }
//
//    private void validOrderSignEs(OrderSignHead orderSignHead, String logSign) {
//        OrderSignHead orderSignHeadDB = new OrderSignHead();
//        orderSignHeadDB.setReceiptOrderId(orderSignHead.getReceiptOrderId());
//        orderSignHeadDB.setIsValid(0);
//
//        if (signOrderService.setValid(orderSignHeadDB, logSign)) {
//            esProxy.editIndex(orderSignHeadDB, logSign);
//        }
//
//    }
//
//    private void validOrderRoEs(OrderRoHead orderRoHead, String logSign) {
//
//        OrderRoHead orderRoHeadDB = new OrderRoHead();
//        orderRoHeadDB.setReturnOrderId(orderRoHead.getReturnOrderId());
//        orderRoHeadDB.setIsValid(0);
//        if (roOrderService.setValidReturn(orderRoHeadDB) > 0) {
//            esProxy.editIndex(orderRoHeadDB, logSign);
//        }
//
//    }

    /**
     * 创建部分返仓单
     *
     * @param orderHead 订单头信息
     * @param logSign   唯一标记
     * @return 返回值
     * @throws Exception 异常信息
     */
//    @Transactional
//    public void createROOrder(CalOrderReturnContent returnOrder, OrderHead orderHead, String logSign, Long venderId) {
//
//        Long orderCode = orderHead.getOrderCode();
//        if (returnOrder == null) {
//            logger.info(orderCode + "没有返仓单详细详细");
//            return;
//        }
//
//        logger.info("orderCode-" + orderCode + " is " + JSON.toJSONString(returnOrder));
//
//        CalOrderReturnHead head = returnOrder.getHead();
//        List<CalOrderReturnDetail> detailList= returnOrder.getDetail();
//
//        if (head == null ) {
//            logger.info(logSign + ":" + orderCode + "返仓头信息为空");
//            return;
//        }
//
//        if (detailList == null || detailList.isEmpty()) {
//            logger.info(logSign + ":" + orderCode + "返仓列表信息为空");
//            return;
//        }
//
//        logger.info(orderCode + "-head:" +JSON.toJSONString(head));
//        logger.info(orderCode + "-detailList:" + JSON.toJSONString(detailList));
//
////        OrderRoHead orderRoHeadFormDb = returnOrderService.getHead(orderCode);
////        if (orderRoHeadFormDb != null) {
//////            throw new BusinessException(ReturnCode.REQUEST_SUCCESS, "此订单已经生成返仓.请查询");
////        }
//
//        OrderShippingHeadWithBLOBs orderShippingHeadWithBLOBs = shippingService.getBolbs(orderCode);
//        if (orderShippingHeadWithBLOBs == null) {
//            throw new BusinessException(ReturnCode.REQUEST_FAIL, "此订单未生成发货单");
//        }
//
////        OrderSignHead orderSignHead = signOrderService.getSignHead(orderCode);
////        if (orderSignHead == null) {
////            throw new BusinessException(ReturnCode.REQUEST_FAIL, "此订单未生成签收单");
////        }
//
//        OrderRoHead orderRoHead = this.createOrderRoHead(orderShippingHeadWithBLOBs, head, orderHead, venderId);
//        List<OrderRoDetail> orderRoDetailList = this.createOrderRoDetails(orderShippingHeadWithBLOBs, detailList, venderId);
//
//        if (orderRoHead == null) {
//            logger.info(logSign + ":" + orderCode + ": orderRoHead is null");
//            return;
//        }
//
//        if (orderRoDetailList == null || orderRoDetailList.isEmpty()) {
//            logger.info(logSign + ":" + orderCode + ": orderRoDetailList is null ");
//            return;
//        }
//
//        int createFlag = 0;
//        for (int i = 0; i < 2; i++) {
//            try {
//                createFlag = roOrderService.create(orderRoHead, orderRoDetailList, false);
//                break;
//            } catch (Exception e) {
//                logger.warn("[" + orderCode + "]创建返仓单失败" + e.getMessage(), e);
//            }
//        }
//        //通知Es
//        if (createFlag > 0) {
//            this.notifyEs(orderHead.getOrderCode(), logSign, orderShippingHeadWithBLOBs.getId(), false);
//        }
//    }


//    /**
//     * 组织反仓单详细信息
//     *
//     * @param orderRoHead 反仓单金额
//     * @param osh         发货单头信息
//     * @return 反仓单详细信息
//     */
//    private List<OrderRoDetail> createOrderRoDetails(final OrderShippingHeadWithBLOBs osh, OrderRoHead orderRoHead) {
//        BigDecimal money = BigDecimal.ZERO;
//        List<OrderShippingDetail> orderShippingDetailList = shippingService.getDetails(osh.getShippingOrderId());
//        List<OrderRoDetail> orderRoDetailList = new ArrayList<>(orderShippingDetailList.size());
//        int ts = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//
//        for (OrderShippingDetail orderShippingDetail : orderShippingDetailList) {
//            OrderRoDetail orderRoDetail = new OrderRoDetail();
//            orderRoDetail.setSkuId(orderShippingDetail.getSkuId());
//            orderRoDetail.setSkuName(orderShippingDetail.getSkuName());
//            orderRoDetail.setItemId(orderShippingDetail.getItemId());
//            orderRoDetail.setSaleUnit(orderShippingDetail.getSaleUnit());
//            orderRoDetail.setQty(orderShippingDetail.getQty());
//            orderRoDetail.setRealQty(orderShippingDetail.getRealQty());
//            orderRoDetail.setStatus(orderShippingDetail.getStatus());
//            orderRoDetail.setExt(orderShippingDetail.getExt());
//            orderRoDetail.setCreatedAt(ts);
//            orderRoDetail.setUpdatedAt(ts);
//            orderRoDetail.setZoneId(orderShippingDetail.getZoneId());
//
//            orderRoDetail.setPrice(orderShippingDetail.getPrice());
//            orderRoDetail.setMoney(orderShippingDetail.getMoney());
//            // 2019-07-10 peter
//            orderRoDetail.setItemCode(orderShippingDetail.getItemCode());
//            //商品返仓备注
//            orderRoDetail.setReturnNote(0);
//            //补偿原因
//            orderRoDetail.setMakeUpNote("");
//            orderRoDetail.setTmsId(0L);
//            if (orderRoHead.getZoneId() == 1000) {
//                orderRoDetail.setSupplierOrg(2);
//            } else {
//                orderRoDetail.setSupplierOrg(3);
//            }
//
//            orderRoDetailList.add(orderRoDetail);
//            money = money.add(orderRoDetail.getQty().multiply(orderRoDetail.getPrice()));
//        }
//
//        orderRoHead.setMoney(money);
//
//        return orderRoDetailList;
//    }

//    private List<OrderRoDetail> createOrderRoDetails(final OrderShippingHeadWithBLOBs osh, List<CalOrderReturnDetail> detailList, Long venderid) {
//
//        List<OrderShippingDetail> orderShippingDetailList = shippingService.getDetails(osh.getShippingOrderId());
//
//        List<OrderRoDetail> orderRoDetailList = new ArrayList<>(orderShippingDetailList.size());
//        int ts = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//
//        Map<String, OrderShippingDetail> code2ShipDetailMap = new HashMap<>();
//        for (OrderShippingDetail orderShippingDetail : orderShippingDetailList) {
//            code2ShipDetailMap.put(orderShippingDetail.getCode(), orderShippingDetail);
//        }
//
//        OrderRoDetail orderRoDetail;
//        JSONObject ext;
//        OrderShippingDetail orderShippingDetail;
//        for (CalOrderReturnDetail detail : detailList) {
//            if (detail == null) {
//                logger.info("反仓单信息详情为空");
//                continue;
//            }
//
//            ext =detail.getExt();
//            String wmCode = ext.getString("wm_code");
//
//            orderShippingDetail = code2ShipDetailMap.get(wmCode);
//
//            orderRoDetail = new OrderRoDetail();
//            orderRoDetail.setSkuId(detail.getSkuId());
//            orderRoDetail.setSkuName(detail.getSkuName());
//            orderRoDetail.setItemId(orderShippingDetail.getItemId());
//            orderRoDetail.setSaleUnit(detail.getSaleUnit());
//            orderRoDetail.setQty(detail.getQty());
//            orderRoDetail.setRealQty(detail.getRealQty());
//            orderRoDetail.setStatus(orderShippingDetail.getStatus());
//            orderRoDetail.setExt(detail.getExt().toJSONString());
//            orderRoDetail.setCreatedAt(ts);
//            orderRoDetail.setUpdatedAt(ts);
//            orderRoDetail.setZoneId(orderShippingDetail.getZoneId());
//            orderRoDetail.setPrice(detail.getPrice());
//            orderRoDetail.setMoney(detail.getMoney());
//            //商品返仓备注
//            orderRoDetail.setReturnNote(0);
//            orderRoDetail.setMakeUpNote("");
//            orderRoDetail.setTmsId(0L);
//
//            Integer itemCode = detail.getItemCode();
//            if (null == itemCode) {
//                itemCode = 0;
//            }
//            orderRoDetail.setItemCode(itemCode);
//
//            orderRoDetail.setSupplierOrg(1);
//
//            orderRoDetail.setVenderId(venderid);
//
//            orderRoDetailList.add(orderRoDetail);
//        }
//
//        return orderRoDetailList;
//    }

//    /**
//     * 组织反仓单头信息
//     *
//     * @param orderShippingHeadWithBLOBs 发货单头信息
//     * @return 反仓单头信息
//     */
//    private OrderRoHead createOrderRoHead(final OrderShippingHeadWithBLOBs orderShippingHeadWithBLOBs, final OrderHead orderHead) {
//
//        int ts = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//        OrderRoHead orderRoHead = new OrderRoHead();
//        orderRoHead.setShippingOrderId(orderShippingHeadWithBLOBs.getShippingOrderId());
//        orderRoHead.setFOrderId(0L);
//        orderRoHead.setType(1);//1-正常返仓,2-异常返仓
//        orderRoHead.setIsMp(YN.N.getValue());
//        orderRoHead.setUid(orderShippingHeadWithBLOBs.getUid());
//        orderRoHead.setAddressId(orderShippingHeadWithBLOBs.getAddressId());
//        orderRoHead.setProvince(orderShippingHeadWithBLOBs.getProvince());
//        orderRoHead.setCity(orderShippingHeadWithBLOBs.getCity());
//        orderRoHead.setCounty(orderShippingHeadWithBLOBs.getCounty());
//        orderRoHead.setArea(orderShippingHeadWithBLOBs.getArea());
//        orderRoHead.setWarehouseId(orderShippingHeadWithBLOBs.getWarehouseId());
//        orderRoHead.setWarehouseName(orderShippingHeadWithBLOBs.getWarehouseName());
//        orderRoHead.setOrderId(orderShippingHeadWithBLOBs.getOrderId());
//        orderRoHead.setShippingOrderId(orderShippingHeadWithBLOBs.getShippingOrderId());
//        //初始化
//        orderRoHead.setStatus(ReturnStatus.Return_Success.getIndex());
//        orderRoHead.setSkuCount(orderShippingHeadWithBLOBs.getSkuCount());
//        orderRoHead.setRealSkuCount(orderShippingHeadWithBLOBs.getRealSkuCount());
//        orderRoHead.setActivedAt(ts);
//        orderRoHead.setReturnAt(ts);
//        orderRoHead.setIsValid(1); //有效
//        orderRoHead.setCreatedAt(ts);
//        orderRoHead.setUpdatedAt(ts);
//        orderRoHead.setReturnType(1);//返仓类型：1-全部返仓；2-部分返仓
//        orderRoHead.setItemType(1);//1-正常；2-包含寄售
//        orderRoHead.setZoneId(orderShippingHeadWithBLOBs.getZoneId());
//        orderRoHead.setStorageTypeId(orderShippingHeadWithBLOBs.getStorageTypeId());
//        orderRoHead.setOrderFrom(orderShippingHeadWithBLOBs.getOrderFrom());
//        orderRoHead.setCostMoney(BigDecimal.ZERO);//和寄售一样设置为零
//        orderRoHead.setSoReturnId("");//返仓SO单号
//        orderRoHead.setCostNtMoney(BigDecimal.ZERO);//和寄售一样设置为零
//        orderRoHead.setReturnNote(0); //全单返仓备注
//        orderRoHead.setPassAt(0);//返仓单标记通过时间(数据组专用)
//        orderRoHead.setReturnOrderType(1);//返仓单类型：1-现场返仓；2-售后返仓
//        orderRoHead.setTmsId(0L);//空
//
//        JSONObject shippintExtObject = JSONObject.parseObject(orderShippingHeadWithBLOBs.getExt()).getJSONObject("head_info");
//        JSONObject returnExtObject = new JSONObject();
//        returnExtObject.put("money", shippintExtObject.get("money"));
//        returnExtObject.put("sku_count", shippintExtObject.get("sku_count"));
//        returnExtObject.put("real_sku_count", shippintExtObject.get("real_sku_count"));
//        JSONObject extObject = new JSONObject();
//        extObject.put("head_info", returnExtObject);
//        orderRoHead.setExt(extObject.toJSONString());
//
//        orderRoHead.setOrderId(orderHead.getOrderCode());
//        orderRoHead.setUid(orderHead.getUserCode());
//        orderRoHead.setAddressId(orderHead.getAddressCode());
//        orderRoHead.setProvince(orderHead.getProvince() + "");
//        orderRoHead.setCity(orderHead.getCity() + "");
//        orderRoHead.setCounty(orderHead.getDistrict() + "");
//        orderRoHead.setArea(orderHead.getArea() + "");
//        orderRoHead.setWarehouseId(orderShippingHeadWithBLOBs.getWarehouseId());
//        orderRoHead.setWarehouseName(orderShippingHeadWithBLOBs.getWarehouseName());
//
//        return orderRoHead;
//    }
//
//    /**
//     * 组织反仓单头信息
//     *
//     * @param orderShippingHeadWithBLOBs 发货单头信息
//     * @return 反仓单头信息
//     */
//    private OrderRoHead createOrderRoHead(final OrderShippingHeadWithBLOBs orderShippingHeadWithBLOBs, CalOrderReturnHead head, final OrderHead orderHead, Long venderId) {
//
//        int ts = (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
//        OrderRoHead orderRoHead = new OrderRoHead();
//        orderRoHead.setShippingOrderId(orderShippingHeadWithBLOBs.getShippingOrderId());
//        orderRoHead.setFOrderId(0L);
//        orderRoHead.setType(1);//1-正常返仓,2-异常返仓
//        orderRoHead.setIsMp(YN.N.getValue());
//        orderRoHead.setUid(orderShippingHeadWithBLOBs.getUid());
//        orderRoHead.setAddressId(orderShippingHeadWithBLOBs.getAddressId());
//        orderRoHead.setProvince(orderShippingHeadWithBLOBs.getProvince());
//        orderRoHead.setCity(orderShippingHeadWithBLOBs.getCity());
//        orderRoHead.setCounty(orderShippingHeadWithBLOBs.getCounty());
//        orderRoHead.setArea(orderShippingHeadWithBLOBs.getArea());
//        orderRoHead.setWarehouseId(orderShippingHeadWithBLOBs.getWarehouseId());
//        orderRoHead.setWarehouseName(orderShippingHeadWithBLOBs.getWarehouseName());
//        orderRoHead.setOrderId(orderShippingHeadWithBLOBs.getOrderId());
//        orderRoHead.setShippingOrderId(orderShippingHeadWithBLOBs.getShippingOrderId());
//        orderRoHead.setStatus(ReturnStatus.Return_Success.getIndex()); //初始化
//
//        orderRoHead.setActivedAt(ts);
//        orderRoHead.setReturnAt(ts);
//        orderRoHead.setIsValid(1); //有效
//        orderRoHead.setCreatedAt(ts);
//        orderRoHead.setUpdatedAt(ts);
//        orderRoHead.setReturnType(2);//返仓类型：1-全部返仓；2-部分返仓
//        orderRoHead.setItemType(1);//1-正常；2-包含寄售
//        orderRoHead.setZoneId(orderShippingHeadWithBLOBs.getZoneId());
//        orderRoHead.setStorageTypeId(orderShippingHeadWithBLOBs.getStorageTypeId());
//        orderRoHead.setOrderFrom(orderShippingHeadWithBLOBs.getOrderFrom());
//        orderRoHead.setCostMoney(BigDecimal.ZERO);//和寄售一样设置为零
//        orderRoHead.setSoReturnId("");//返仓SO单号
//        orderRoHead.setCostNtMoney(BigDecimal.ZERO);//和寄售一样设置为零
//        orderRoHead.setReturnNote(0); //全单返仓备注
//        orderRoHead.setPassAt(0);//返仓单标记通过时间(数据组专用)
//        orderRoHead.setReturnOrderType(1);//返仓单类型：1-现场返仓；2-售后返仓
//        orderRoHead.setTmsId(0L);//空
//        orderRoHead.setVenderId(venderId);
//
//        orderRoHead.setSkuCount(head.getSkuCount());
//        orderRoHead.setRealSkuCount(head.getRealSkuCount());
//        orderRoHead.setExt(head.getExt().toJSONString());
//        orderRoHead.setMoney(head.getMoney());
//
//        orderRoHead.setOrderId(orderHead.getOrderCode());
//        orderRoHead.setUid(orderHead.getUserCode());
//        orderRoHead.setAddressId(orderHead.getAddressCode());
//        orderRoHead.setProvince(orderHead.getProvince() + "");
//        orderRoHead.setCity(orderHead.getCity() + "");
//        orderRoHead.setCounty(orderHead.getDistrict() + "");
//        orderRoHead.setArea(orderHead.getArea() + "");
//        orderRoHead.setWarehouseId(orderShippingHeadWithBLOBs.getWarehouseId());
//        orderRoHead.setWarehouseName(orderShippingHeadWithBLOBs.getWarehouseName());
//
//        return orderRoHead;
//    }
//
//    /**
//     * 通知es
//     *
//     * @param orderCode           订单号
//     * @param logSign             唯一标识
//     * @param orderShoppingHeadId 发货单头细信息
//     */
//    private void notifyEs(final Long orderCode, final String logSign, Long orderShoppingHeadId, boolean allFlag) {
//
//        OrderRoHead orderRoHead = roOrderService.getHead(orderCode);
//        if (orderRoHead == null) {
//            logger.info("[" + orderCode + "]" + "返仓单查询失败");
//        }
//        try {
//            logger.info(logSign + "ES返仓新建入参是 -- " + JsonUtils.obj2Json(orderRoHead));
//            esProxy.createIndex(orderRoHead, logSign);
//            if (allFlag) {
//                OrderShippingHeadWithBLOBs oshEs = new OrderShippingHeadWithBLOBs();
//                oshEs.setShippingOrderId(orderRoHead.getShippingOrderId());
//                oshEs.setOrderId(orderCode);
//                oshEs.setId(orderShoppingHeadId);
//                oshEs.setStatus(2);
//                logger.info(logSign + "ES返仓更新发货单入参是 -- " + JsonUtils.obj2Json(oshEs));
//                esProxy.editIndex(oshEs, logSign);
//                OrderHeadForEs ohEs = new OrderHeadForEs();
//                ohEs.setOrderCode(orderCode);
//                ohEs.setOrderStatus(OrderStatus.ORDER_SIGN.getIndex());
//                logger.info(logSign + "ES返仓更新订单入参是 -- " + JsonUtils.obj2Json(ohEs));
//                esProxy.editIndex(ohEs, logSign);
//            }
//        } catch (Exception e) {
//            logger.info(logSign + "订单号" + orderCode + ",生成发货单同步es失败。");
//            logger.info(e.getMessage(), e);
//        }
//
//    }
}
